package com.gtotek.tumchu.activity;

import org.sufficientlysecure.htmltextview.HtmlTextView;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxStatus;
import com.androidquery.callback.BitmapAjaxCallback;
import com.androidquery.callback.ImageOptions;
import com.gtotek.tumchu.R;
import com.gtotek.tumchu.adapter.DecodingAdapter;
import com.gtotek.tumchu.base.Constans;
import com.gtotek.tumchu.dao.QuestionEntity;
import com.gtotek.tumchu.util.SoundUtil;

public class WinJavActivity extends Activity {

	private GridView mGrvWin;

	private ImageView mImgActor;

	private HtmlTextView mTvContent;

	private Context mContext = this;

	private ProgressBar mProgressBar;

	private TextView mTvContinue;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_win_jav);

		mGrvWin = (GridView) this.findViewById(R.id.gv_win);
		mImgActor = (ImageView) this.findViewById(R.id.imgActor);
		mTvContent = (HtmlTextView) this.findViewById(R.id.tvContent);
		mProgressBar = (ProgressBar) this.findViewById(R.id.progressBar);

		mTvContinue = (TextView) this.findViewById(R.id.tv_jav_continue);
		mTvContinue.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		Bundle bundle = getIntent().getExtras();

		QuestionEntity questionEntity = (QuestionEntity) bundle
				.getSerializable(Constans.KEY_QUESTIONENTITY);

		DecodingAdapter decodingAdapter = new DecodingAdapter(mContext,
				questionEntity);

		mGrvWin.setAdapter(decodingAdapter);

		if (questionEntity.getContent().trim().equals(""))
			mTvContent.setText(getResources()
					.getText(R.string.jav_default_info));
		else
			mTvContent.setHtmlFromString(questionEntity.getContent(), false);

		ImageOptions imageOptions = new ImageOptions();
		imageOptions.fileCache = true;
		imageOptions.memCache = true;

		BitmapAjaxCallback ajaxCallback = new BitmapAjaxCallback() {
			@Override
			public void async(Context context) {
				// TODO Auto-generated method stub
				super.async(context);
				mProgressBar.setVisibility(View.VISIBLE);
			}

			@Override
			protected void callback(String url, ImageView iv, Bitmap bm,
					AjaxStatus status) {
				// TODO Auto-generated method stub
				super.callback(url, iv, bm, status);
				mProgressBar.setVisibility(View.GONE);
			}
		};

		AQuery aQuery = new AQuery(mContext);
		aQuery.id(mImgActor)
				.progress(mProgressBar)
				.image(questionEntity.getImage(), true, true, 0,
						R.drawable.img_question, ajaxCallback);
		
		SoundUtil.hexat(mContext, SoundUtil.SFX_PASS);
	}
}
